import 'package:earclean/app/modules/message/controllers/message_controller.dart';
import 'package:earclean/app/modules/mine/controllers/mine_controller.dart';
import 'package:earclean/app/modules/technician/controllers/technician_controller.dart';
import 'package:earclean/app/request/request_main.dart';
import 'package:earclean/app/routes/app_pages.dart';
import 'package:earclean/app/util/common_utils.dart';
import 'package:earclean/app/util/string_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

class TableController extends GetxController {
  final currentIndex = 0.obs;
  final count = ''.obs;
  final showTabbar = true.obs;
  final PageController pageController = PageController(
    initialPage: 0,
    keepPage: true,
  );

  @override
  void onInit() {
    super.onInit();
  }

  loadunm() async {
    String key = box.read('key') ?? "";
    if (isNotEmpty(key)) {
      Map map = await RequestMain.getUrl({},
          turl: 'https://fjbamz.jiliol.net/sign/?a=unread');
      if (map.isNotEmpty && map['data'] is Map && map['data'].isNotEmpty) {
        int unread = map['data']['unread'];
        if (unread > 9) {
          count.value = '9+';
        } else if (unread == 0) {
          count.value = '';
        } else {
          count.value = '$unread';
        }
      }
      // count.value =
    }
  }

  @override
  void onReady() {
    dynamic ttab = box.read("tab");
    if (ttab != null && ttab < 4) {
      // currentIndex.value = ttab;
      changePage(1);
    }

    super.onReady();
  }

  @override
  void onClose() {}
  void changePage(int index) {
    dynamic key = box.read("key");
    if (index == 1) {
      if (null == key) {
        Get.offAllNamed(Routes.LOGIN);
      } else {
        currentIndex.value = index;
        pageController.jumpToPage(index);
        Get.find<TechnicianController>().loadUrl();
      }
    } else if (index == 3) {
      if (null == key) {
        Get.offAllNamed(Routes.LOGIN);
      } else {
        currentIndex.value = index;
        pageController.jumpToPage(index);
        Get.find<MessageController>().loadData();
      }
    } else if (index == 4) {
      if (null == key) {
        Get.offAllNamed(Routes.LOGIN);
      } else {
        currentIndex.value = index;
        pageController.jumpToPage(index);
        Get.find<MineController>().loadData();
      }
    } else {
      currentIndex.value = index;
      pageController.jumpToPage(index);
    }
    dynamic ttab = box.read("tab");
    bool qidong = box.read("qidong");
    // 弹框
    dynamic rank = box.read("rank");
    if ((rank.toString() == '1' && ttab == index && qidong == true)) {
      box.write("qidong", false);
      channel.invokeMethod('toscore');
    }
  }
}
